from rest_framework import serializers

from django.conf import settings

from prj004.models import (
    info, clinical, cure, history,
    relevant, result, summary
)
from utils.savemobile import create_object
from utils.samecode import ExtendsSerializer, SCSerializer


class InfoLinkSerializer(ExtendsSerializer, serializers.HyperlinkedModelSerializer):
    check = serializers.HyperlinkedIdentityField(read_only=True, view_name='prj004-info-check', lookup_field='pk',)
    summary = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-summary-detail')
    history = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-history-detail')
    relevant = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-relevant-detail')
    clinical = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-clinical-detail')
    cure = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-cure-detail')
    result = serializers.HyperlinkedRelatedField(read_only=True, view_name='prj004-result-detail')
    info = serializers.HyperlinkedIdentityField(read_only=True, view_name='prj004-info-detail', lookup_field='pk')
    owner_id = serializers.PrimaryKeyRelatedField(label='辅助医生ID', read_only=True)
    owner_name = serializers.StringRelatedField(label='辅助医生姓名', read_only=True, source='owner.user_name')
    create_time = serializers.DateTimeField(format=settings.DATETIME_FORMAT, read_only=True)

    class Meta:
        model = info.Info
        project_name = 'prj004'
        save_mobile = False
        fields = '__all__'
        read_only_fields = ['owner', 'serial', 'check_status', 'reason_for_check', 'create_time']
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-info-detail',
                'lookup_field': 'pk'
            }
        }


class InfoSerializer(ExtendsSerializer, serializers.ModelSerializer):

    class Meta:
        model = info.Info
        exclude = ['create_time', 'id']
        read_only_fields = ['serial', 'bmi', 'whr', 'check_status', 'reason_for_check']
        project_name = 'prj004'
        save_mobile = True


class ClinicalLinkSerializer(SCSerializer):

    class Meta:
        model = clinical.Clinical
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-clinical-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class ClinicalSerializer(serializers.ModelSerializer):

    class Meta:
        model = clinical.Clinical
        exclude = ['owner', 'create_time', 'info', 'id']


class CureLinkSerializer(SCSerializer):

    class Meta:
        model = cure.Cure
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-cure-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class CureSerializer(serializers.ModelSerializer):

    class Meta:
        model = cure.Cure
        project_name = 'prj004'
        exclude = ['owner', 'create_time', 'info', 'id']


class HistoryLinkSerializer(SCSerializer):

    class Meta:
        model = history.History
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-history-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class HistorySerializer(serializers.ModelSerializer):

    class Meta:
        model = history.History
        exclude = ['owner', 'create_time', 'info', 'id']


class RelevantLinkSerializer(SCSerializer):

    class Meta:
        model = relevant.Relevant
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-relevant-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class RelevantSerializer(serializers.ModelSerializer):

    class Meta:
        model = relevant.Relevant
        exclude = ['owner', 'create_time', 'info', 'id']


class ResultLinkSerializer(SCSerializer):

    class Meta:
        model = result.Result
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-result-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class ResultSerializer(serializers.ModelSerializer):

    class Meta:
        model = result.Result
        exclude = ['owner', 'create_time', 'info', 'id']


class SummaryLinkSerializer(SCSerializer):

    class Meta:
        model = summary.Summary
        fields = '__all__'
        project_name = 'prj004'
        read_only_fields = ['owner', ]
        extra_kwargs = {
            'url': {
                'view_name': 'prj004-summary-detail',
                'lookup_field': 'pk'
            },
            'info': {
                'view_name': 'prj004-info-detail',
            }
        }


class SummarySerializer(serializers.ModelSerializer):

    class Meta:
        model = summary.Summary
        exclude = ['owner', 'create_time', 'info', 'id']


class SearchSerializer(serializers.ModelSerializer):
    """ For search api """
    patient_birth = serializers.CharField(required=False, help_text='出生年份')
    career = serializers.CharField(required=False, help_text='职业')
    patient_name = serializers.CharField(required=False, help_text='姓名')
    patient_phone = serializers.CharField(required=False, help_text='手机号')
    hospital_name = serializers.CharField(required=False, help_text='所在医院')
    # address = serializers.CharField(required=False, help_text='地址')
    check_status = serializers.CharField(required=False, help_text='审核状态')

    # 针对用户进行查询
    owner__user_name = serializers.CharField(help_text='医生姓名', required=False)
    owner__hospital = serializers.CharField(help_text='医生所在医院', required=False)

    class Meta:
        model = info.Info
        fields = [
            'patient_name', 'patient_phone', 'hospital_name',
            'patient_birth', 'career', 'check_status',
            'owner__user_name', 'owner__hospital'
        ]


class CheckStatusSerializer(serializers.Serializer):
    CHECKED_STATUS = [i[0] for i in info.Info.CHECKED_CHOICE]
    CHECKED_CHOICE = info.Info.CHECKED_CHOICE

    check_status = serializers.ChoiceField(required=True, choices=CHECKED_CHOICE, help_text='审核状态')
    reason_for_check = serializers.CharField(required=False, help_text='审核不通过原因')

    def validate_check_status(self, value):
        if value not in self.CHECKED_STATUS:
            raise serializers.ValidationError('非法审核状态')

        return value


class MobileSerializer(serializers.Serializer):
    info = InfoSerializer(required=True)
    clinical = ClinicalSerializer(required=False)
    cure = CureSerializer(required=False)
    history = HistorySerializer(required=False)
    summary = SummarySerializer(required=False)
    relevant = RelevantSerializer(required=False)
    result = ResultSerializer(required=False)

    def create(self, instance):
        info = instance.get('info')
        info_owner = info.pop('owner')
        info['owner'] = info_owner.id

        info_serializer = InfoSerializer(data=info)
        info_serializer.is_valid(raise_exception=True)
        info_instance = info_serializer.save()

        summary = instance.get('summary')
        history = instance.get('history')
        cure = instance.get('cure')
        clinical = instance.get('clinical')
        relevant = instance.get('relevant')
        result = instance.get('result')

        create_object(info_instance, summary, SummarySerializer)
        create_object(info_instance, history, HistorySerializer)
        create_object(info_instance, cure, CureSerializer)
        create_object(info_instance, clinical, ClinicalSerializer)
        create_object(info_instance, result, ResultSerializer)
        create_object(info_instance, relevant, RelevantSerializer)

        return instance


class AllSerializer(serializers.Serializer):
    info = InfoSerializer(required=False)
    history = HistorySerializer(required=False)
    clinical = ClinicalSerializer(required=False)
    cure = CureSerializer(required=False)
    summary = SummarySerializer(required=False)
    result = ResultSerializer(required=False)
    relevant = RelevantSerializer(required=False)
